<template>
  <div class="add-parallel">
    <el-dialog
      :title="visible.type===0? '添加平行链':'修改平行链'"
      :visible.sync="visible.showSetDialog"
      width="600px"
      center
      :close-on-click-modal="false"
    >
      <div class="content">
        <div class="line flex-left">
          <label class="title">平行链名称</label>
          <el-input v-model="param.platform" size="medium" placeholder="请输入平行链名称"></el-input>
        </div>
        <div class="line flex-left">
          <label class="title">基础代币</label>
          <el-input v-model="param.token" size="medium" placeholder="请输入基础代币，例：BTC"></el-input>
        </div>
        <div class="line flex-left">
          <label class="title">代扣地址</label>
          <el-input v-model="param.address" size="medium" placeholder="请输入代扣地址"></el-input>
        </div>
        <div class="line flex-left">
          <label class="title">代扣私钥</label>
          <el-input v-model="param.private_key" size="medium" placeholder="请输入代扣私钥"></el-input>
        </div>
        <div class="line flex-left">
          <label class="title">
            手续费
            <br>(基础代币)
          </label>
          <el-input v-model="param.fee" size="medium" placeholder="请输入手续费"></el-input>
        </div>
        <div class="line flex-left">
          <label class="title">执行器</label>
          <el-input v-model="param.exer" size="medium" placeholder="请输入执行器"></el-input>
        </div>
        <div class="line flex-left">
          <label class="title">浏览器链接</label>
          <el-input v-model="param.brower_url" size="medium" placeholder="请输入浏览器链接"></el-input>
        </div>
        <div class="line flex-left">
          <label class="title">转币支持</label>
          <el-radio v-model="param.lock" :label="1">不允许转币</el-radio>
          <el-radio v-model="param.lock" :label="2">允许转币</el-radio>
        </div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="visible.showSetDialog = false">取 消</el-button>
        <el-button
          size="small"
          type="primary"
          @click="submit"
          :disabled="onSubmit"
          :loading="onSubmit"
        >确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  createParaList,
  updateParaList
} from "../../views/WalletManage/Wallet.service.js";
import { getParaList } from "../../views/common.service";
export default {
  props: ["visible"],
  data() {
    return {
      param: {
        platform: "",
        address: "",
        private_key: "",
        exer: "",
        fee: "",
        brower_url: "",
        token: "",
        lock: 1
      },
      onSubmit: false
    };
  },
  watch: {
    visible: {
      handler(item) {
        if (item.type === 1) this.param = JSON.parse(JSON.stringify(item.data));
        if (item.showSetDialog === false)
          this.param = {
            platform: "",
            address: "",
            private_key: "",
            exer: "",
            fee: "",
            brower_url: "",
            token: "",
            lock: 1
          };
      },
      deep: true
    }
  },
  methods: {
    async submit() {
      this.param.fee = parseFloat(this.param.fee);
      const res =
        this.visible.type === 0
          ? await createParaList(this.param)
          : await updateParaList(this.param);
      if (res.code === 0) {
        this.$notify.success({
          title: "提示",
          message: this.visible.type === 0 ? "添加成功" : "修改成功"
        });
        const res = await getParaList();
        if (res && res.code === 0) {
          this.$store.commit("updataParallel", res.data.items);
        } else {
          this.$store.commit("updataParallel", []);
        }
        this.$emit("success");
      }
    }
  }
};
</script>

<style lang="scss">
.add-parallel {
  .el-dialog__header {
    border-bottom: 1px solid #e9e9e9;
    padding: 15px;
    font-weight: 600;
  }
  .content {
    .line {
      height: 60px;
      .title {
        width: 6em;
        text-align: right;
        margin-right: 20px;
        flex-shrink: 0;
      }
    }
  }
}
</style>